﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using ValidationFramework.Reflection;

namespace ValidationFramework
{
	public class FieldKey
	{
		RuntimeFieldHandle _fieldHandle { get; set; }

		/// <summary>
		/// Initializes a new instance of the PropertyKey class.
		/// </summary>
		public FieldKey(RuntimeFieldHandle fieldHandle)
		{
			_fieldHandle = fieldHandle;
		}

		public FieldInfo ResolveFieldInfo()
		{
			return FieldInfo.GetFieldFromHandle(_fieldHandle);
		}

		/// <summary>
		/// Retrieves a typeKey via reflection
		/// </summary>
		public TypeKey TypeKey
		{
			get
			{
				return new TypeKey(ResolveFieldInfo().ReflectedType.TypeHandle);
			}
		}
		/// <summary>
		/// Uses reflection to get the represented field type's runtimeTypeHandle
		/// </summary>
		/// <returns></returns>
		/// <remarks></remarks>
		public RuntimeTypeHandle ResolveFieldTypeHandle()
		{
			var field = FieldInfo.GetFieldFromHandle(_fieldHandle);
			return field.FieldType.TypeHandle;
		}

		// --- For Dictionary Support
		public override bool Equals(object obj)
		{
			var key = (FieldKey)obj;
			return this._fieldHandle.Equals(key._fieldHandle);
		}

		public override int GetHashCode()
		{
			return _fieldHandle.GetHashCode();
		}
	}
}
